import { Config } from "../../config/Config";
import DateUtil from "../../engine/utils/DateUtil";
import { HttpUtil } from "../../engine/utils/HttpUtil";
import { Logger } from "../../engine/utils/Logger";

const request = require('request');

export class GameHttpHelper {

    public static Game_url: string = "http://127.0.0.1:9230"

    public static init() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     this.Game_url = "http://127.0.0.1:9231"
        // }
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     this.Game_url = "http://127.0.0.1:9231"
        // }
        // else if (Config.environment == Config.EnvironmentType.Jk) {
        //     this.Game_url = "http://127.0.0.1:9235"
        // }
        // else if (Config.environment == Config.EnvironmentType.chaoyou) {
        //     this.Game_url = "http://127.0.0.1:9233"
        // }
        // else {
            this.Game_url = "http://127.0.0.1:9236"
        // }
    }

    public static get_online_num(url: string) {
        return new Promise((resolve, reject) => {
            HttpUtil.request_by_body(url, {}, (response) => {
                if (response["status"] == 200) {
                    resolve(response["data"])
                }
            })
        })
    }

    public static async onlinenum() {
        let data = {
            "mod": "admin",
            "func": "onlinenum",
        };
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("onlinenum==resp==", res)
        return res["data"]
    }

    public static async charge_by_gm(id: number, num, mod: string = "gem") {
        let data = {
            "mod": mod,
            "func": "add",
            "rid": id,
            "num": num,
        };
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("charge_by_gm==resp==", res)
        return res["data"]
    }

    // http://127.0.0.1:9230?mod=result&func=change&r1=1&r2=1&operator=acc&tableid=1
    public static async yuxiaxie_control(touzi1: number, touzi2: number, operator: string, tableid: number) {
        let data = {
            "mod": "result",
            "func": "change",
            "r1": touzi1,
            "r2": touzi2,
            "operator": operator,
            "tableid": tableid,
        };
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("yuxiaxie_control==resp==", res)
        return res["data"]
    }

    public static async group_list() {
        let data = {
            "mod": "log",
            "func": "groupList",
        };
        Logger.log("group_list==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("group_list==resp==", res)
        return res["data"]
    }

    public static async group_member(groupid: number) {
        let data = {
            "mod": "log",
            "func": "groupMemberList",
            "groupid": groupid,
        };
        Logger.log("group_member==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("group_member==resp==", res)
        return res["data"]
    }

    public static async groupTableList(groupid: number) {
        let data = {
            "mod": "log",
            "func": "groupTableList",
            "groupid": groupid,
        };
        Logger.log("groupTableList==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("groupTableList==resp==", res)
        return res["data"]
    }

    public static async groupZhanji(groupid: number, time: number) {
        let data = {
            "mod": "log",
            "func": "groupZhanji",
            "groupid": groupid,
            "time": time,
        };
        Logger.log("groupZhanji==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("groupZhanji==resp==", res)
        return res["data"]
    }

    public static async groupZhanjiOne(groupid: number, time: number, userid: number) {
        let data = {
            "mod": "log",
            "func": "groupZhanjiOne",
            "groupid": groupid,
            "time1": time,
            "time2": Math.floor(DateUtil.now() / 1000),
            "userid": userid,
        };
        Logger.log("groupZhanjiOne==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("groupZhanjiOne==resp==", res)
        return res["data"]
    }

    public static async groupDiss(groupid: number) {
        let data = {
            "mod": "log",
            "func": "groupDiss",
            "groupid": groupid,
        };
        Logger.log("groupDiss==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("groupDiss==resp==", res)
        return res["data"]
    }

    public static async robot_list() {
        let data = {
            "mod": "robot2",
            "func": "list",
        };
        Logger.log("robot_list==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("robot_list==resp==", res)
        return res["data"]
    }

    public static async robot_create(groupid: number, nick: string, face: string,
         time: number, robotid: number, min: number, max: number,tableid:number) {
        let data = {
            "mod": "robot2",
            "func": "create",
            "groupid": groupid,
            "nick": nick,
            "face": face,
            "time": time,
            "robotid": robotid,
            "min": min,
            "max": max,
            "tableid":tableid,
        };
        if (robotid > 0) {
            data["func"] = "edit"
        }
        Logger.log("robot_create==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("robot_create==resp==", res)
        return res["data"]
    }

    public static async robot_apply(robotid: number) {
        let data = {
            "mod": "robot2",
            "func": "apply",
            "robotid": robotid,
        };
        Logger.log("robot_apply==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("robot_apply==resp==", res)
        return res["data"]
    }

    public static async robot_leave(robotid: number) {
        let data = {
            "mod": "robot2",
            "func": "leave",
            "robotid": robotid,
        };
        Logger.log("robot_leave==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("robot_leave==resp==", res)
        return res["data"]
    }

    public static async getWaigua_list() {
        let data = {
            "mod": "result",
            "func": "white_list",
        };
        Logger.log("getWaigua_list==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("getWaigua_list==resp==", res)
        return res["data"]
    }

    public static async set_waigua_list(udis: string) {
        let data = {
            "mod": "result",
            "func": "add_white",
            "uids": udis
        };
        Logger.log("set_waigua_list==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("set_waigua_list==resp==", res)
        return res["data"]
    }

    public static async set_yuxiaxie_rate(tableid: number, rate: number, operator: string) {
        let data = {
            "mod": "result",
            "func": "rate",
            "operator": operator,
            "tableid": tableid,
            "rate": rate
        };
        Logger.log("set_yuxiaxie_rate==req==", data)
        let res = await HttpUtil.request_by_form_promise(this.Game_url, data)
        Logger.log("set_yuxiaxie_rate==resp==", res)
        return res["data"]
    }

}